using System;
using System.Collections.Generic;
using System.Text;

namespace NSimpleDB.Service.VistaDb.Resultsets
{
    internal class PagedResultset
    {
        private int pageSize;
        private List<ResultsetPage> pages;


        public PagedResultset(int pageSize)
        {
            this.pageSize = pageSize;
            this.pages = new List<ResultsetPage>();
            this.pages.Add(new ResultsetPage());
        }


        public void Add(string result)
        {
            ResultsetPage p = this.pages[this.pages.Count - 1];
            if (p.results.Count == this.pageSize)
            {
                p = p.CreateNextPage();
                this.pages.Add(p);
            }
            p.results.Add(result);
        }


        public string FirstToken
        {
            get
            {
                if (pages[0].results.Count > 0)
                    return this.pages[0].token;
                else
                    return null;
            }
        }


        public IList<ResultsetPage> Pages
        {
            get { return this.pages; }
        }
    }


    internal class ResultsetPage
    {
        public string token;
        public string nextToken;
        public List<string> results;
        public DateTime createdAt;


        public ResultsetPage()
        {
            this.token = Guid.NewGuid().ToString();
            this.results = new List<string>();
            this.createdAt = DateTime.Now;
        }


        public ResultsetPage CreateNextPage()
        {
            ResultsetPage p = new ResultsetPage();
            this.nextToken = p.token;
            return p;
        }
    }
}
